////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.orders.data {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import kiwi.utils.Validation;
	
	import plantool.core.CoreDispatcher;
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.events.OrderEvent;
	
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="rows_loaded", type="plantool.events.OrderEvent")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	[Bindable]
	public class Order extends CoreDispatcher {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _id:uint						= 0;
		private var _rowsLoaded:Boolean				= false;
		private var _meta:Ordermeta					= new Ordermeta();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get id():uint 			{ return _id; }
		public function set id(value:uint):void {
			//store new id
			_id = value; 
			
			//load all rows
			orderApi.getOrderrows(this, onComplete_getOrderrows, onError_getOrderrows);
		}
		public function get rowsLoaded():Boolean 		{ return _rowsLoaded; }
		
		public function set meta(value:Ordermeta):void		{ _meta = value; }
		public function get meta():Ordermeta	 			{ return _meta; }
		
		public var onr:String					= "";
		public var orderrows:Vector.<Orderrow>	= new Vector.<Orderrow>();
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function Order() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_getOrderrows(result:QueryResult):void {
			//store rows
			if (Validation.isSet(result) && Validation.isSet(result.data) && result.data.length > 0) {
				for (var i:uint = 0; i < result.data.length; i++) {
					orderrows.push(result.data[i]);
				}
			}
			
			//load all rows
			orderApi.getOrdermeta(this, onComplete_getOrdermeta, onError_getOrderrows);
		}
		
		private function onComplete_getOrdermeta(result:QueryResult):void {
			//store rows
			if (Validation.isSet(result) && Validation.isSet(result.data) && result.data.length == 1) {
				//store order meta
				_meta = result.data[0];
				
				//set rowsLoaded
				_rowsLoaded = true;
				
				//dispatch event
				dispatchEvent(new OrderEvent(OrderEvent.ROWS_LOADED, _id));
			}
		}
		
		private function onError_getOrderrows(result:QueryErrorResult):void {
			trace("Error getting orderrows:", result.message);
		}
	}
}